<template>
  <div id="addPictureBatchPage">
    <!--    标题-->
    <h2 style="margin-bottom: 16px">批量创建图片</h2>
    <!--    上传图片-->
    <!--    图片信息表单-->
    <a-form layout="vertical" :model="formData" @finish="handleSubmit">
      <a-form-item label="关键词" name="searchText">
        <a-input v-model:value="formData.searchText" allow-clear placeholder="请输入关键词" />
      </a-form-item>
      <a-form-item label="抓取数量" name="count">
        <a-input
          type="number"
          :min="1"
          :max="30"
          style="min-width: 180px"
          v-model:value="formData.count"
          allowClear
          placeholder="请输入需要抓取的图片数量!"
        />
      </a-form-item>
      <a-form-item label="前缀名" name="category">
        <a-input
          v-model:value="formData.namePrefix"
          allow-clear
          placeholder="请输入前缀名"
        ></a-input>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%" :loading="loading"
          >执行任务
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { uploadPictureByBatchUsingPost } from '@/api/tupianguanli.ts'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'

const formData = reactive<API.PictureUploadByBatchRequest>({
  count: 10,
})
const router = useRouter()
const loading = ref<boolean>(false)
/**
 * 提交表单
 *@param values
 */
const handleSubmit = async (values: any) => {
  loading.value = true
  const res = await uploadPictureByBatchUsingPost({
    ...formData,
  })
  try {
    if (res.data.code === 0 && res.data.data) {
      message.success(`创建成功，共 ${res.data.data} 条`)
      router.push({
        path: '/',
      })
    } else {
      message.error('创建失败，' + res.data.message)
    }
  } catch (e) {
    message.error('创建失败，' + e)
  }
  loading.value = false
}
</script>
<style scoped>
#addPictureBatchPage {
  max-width: 720px;
  margin: 0 auto;
}
</style>
